<?php

namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path'), EXT);

use app\admin\Controller;
use think\Db;

class DriverRank extends Controller {

    use \app\admin\traits\controller\Controller;

    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 回收站
     * @return mixed
     */
    public function recyclebin() {
        if ($this->request->param("name")) {
            $map['dr.name'] = ["like", "%" . $this->request->param("name") . "%"];
        }
        $map['tr.depth'] = 1;
        $map['dr.isdelete'] = TRUE;
        // 是否开启分页
        $paginate = isset($map['_paginate']) ? boolval($map['_paginate']) : false;
        if ($paginate) {
            // 分页查询
            // 每页数据数量
            $listRows = $this->request->param('numPerPage') ? : Config::get("paginate.list_rows");
            $list = Db('driver_rank_tree')->alias('tr')
                ->join('tp_driver_rank dr ', 'dr.id=tr.son_id', 'left')
                ->where($map)
                ->paginate($listRows, false, ['query' => $this->request->get()]);


                // 模板赋值显示
                $this->view->assign('list', $list);
                $this->view->assign("count", $list->total());
                $this->view->assign("page", $list->render());
                $this->view->assign('numPerPage', $list->listRows());

        } else {
            // 不开启分页查询
            $list = Db('driver_rank_tree')->alias('tr')
                ->join('tp_driver_rank dr ', 'dr.id=tr.son_id', 'left')
                ->where($map)
                ->select();


                // 模板赋值显示
                $this->view->assign('list', $list);
                $this->view->assign("count", count($list));
                $this->view->assign("page", '');
                $this->view->assign('numPerPage', 0);

        }
        return $this->view->fetch();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index() {
        if ($this->request->param("name")) {
            $map['d.name'] = ["like", "%" . $this->request->param("name") . "%"];
        }
        $map['tr.depth'] = 1;
        $map['d.isdelete'] = false;


        // 是否开启分页
        $paginate = isset($map['_paginate']) ? boolval($map['_paginate']) : false;
        if ($paginate) {
            // 分页查询
            // 每页数据数量
            $listRows = $this->request->param('numPerPage') ? : Config::get("paginate.list_rows");
            $list = Db('driver_rank_tree')->alias('tr')
                ->join('tp_driver_rank d ', 'd.id=tr.son_id', 'left')
                ->where($map)
                ->order("tr.left asc")
                ->paginate($listRows, false, ['query' => $this->request->get()]);


                // 模板赋值显示
                $this->view->assign('list', $list);
                $this->view->assign("count", $list->total());
                $this->view->assign("page", $list->render());
                $this->view->assign('numPerPage', $list->listRows());

        } else {
            // 不开启分页查询
            $list = Db('driver_rank_tree')->alias('tr')
                ->join('tp_driver_rank d ', 'd.id=tr.son_id', 'left')
                ->where($map)
                ->order("tr.left asc")
                ->select();


                // 模板赋值显示
                $this->view->assign('list', $list);
                $this->view->assign("count", count($list));
                $this->view->assign("page", '');
                $this->view->assign('numPerPage', 0);

        }

        return $this->view->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add() {

        if ($this->request->isAjax()) {
            // 插入
            $data = $this->request->except(['id']);
            $data['create_time'] = NOW_TIME;
            $data['update_time'] = NOW_TIME;
            // 简单的直接使用db写入
            $model = model("TreeModel");

            $model->startTrans();
            try {
                $ret = $model->table("tp_driver_rank")->strict(false)->insertGetId($data);
                if ($ret) {
                    $model->treeTable("tp_driver_rank_tree")->moveTo($ret, $data['pid']);
                }

                // 提交事务
                $model->commit();
            } catch (\Exception $e) {
                // 回滚事务
                $model->rollback();
                return ajax_return_adv_error($e->getMessage());
            }
            return ajax_return_adv('添加成功');
        } else {
            // 添加
            $ranks=Db::name('driver_rank')->select();
            $this->view->assign('ranks',$ranks);
            return $this->view->fetch(isset($this->template) ? $this->template : 'edit');
        }
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit() {
        $data = $this->request->param();
        if ($this->request->isAjax()) {
            // 插入
            $data['update_time'] = NOW_TIME;
            // 简单的直接使用db写入
            $model = model("TreeModel");

            $model->startTrans();
            try {
                $ret = $model->table("tp_driver_rank")->strict(false)->where('id',$data['id'])->update($data);
                if ($ret) {
                    $model->treeTable("tp_driver_rank_tree")->moveTo($data['id'], $data['pid']);
                }

                // 提交事务
                $model->commit();
            } catch (\Exception $e) {
                // 回滚事务
                $model->rollback();
                return ajax_return_adv_error($e->getMessage());
            }
            return ajax_return_adv('修改成功');
        } else {

            $map['id']=array('neq',$data['id']);


            $ranks=Db::name('driver_rank')->where($map)->select();
            $this->view->assign('ranks',$ranks);
            $vo=Db::name("driver_rank")->where(array('id'=>$data['id']))->find();
            $pid=Db::name('driver_rank_tree')->where(array('son_id'=>$data['id'],'depth'=>1))->value('parent_id');
            $vo['pid']=$pid;
            $this->view->assign("vo",$vo);
            return $this->view->fetch(isset($this->template) ? $this->template : 'edit');
        }
    }
    /**
     * 永久删除
     */
    public function deleteForever() {

        $ids = $this->request->param("id");
        $ids=  explode(",", $ids);
        $model = model("TreeModel")->treeTable("tp_driver_rank_tree");
        $model->startTrans();
        try {
            if (is_array($ids)) {
                foreach ($ids as $id) {
                    if (false === $model->table('tp_driver_rank')->where(array('id' => $id))->delete()) {
                        return ajax_return_adv_error($model->getError());
                    }
                    $model->delNode($id);
                }
            }
            // 提交事务
            $model->commit();
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return ajax_return_adv_error($e->getMessage());
        }
        return ajax_return_adv("删除成功");
    }

    /**
     * 清空回收站
     */
    public function clear() {
        $model = model("TreeModel")->treeTable("tp_driver_rank_tree");
        $where[$this->fieldIsDelete] = 1;
        $ids = $model->table('tp_driver_rank')->where($where)->column('id');
        $model->startTrans();
        try {
            if (is_array($ids)) {
                foreach ($ids as $id) {
                    if (false === $model->table('tp_driver_rank')->where(array('id' => $id))->delete()) {
                        return ajax_return_adv_error($model->getError());
                    }
                    $model->delNode($id);
                }
            }
            // 提交事务
            $model->commit();
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return ajax_return_adv_error($e->getMessage());
        }

        return ajax_return_adv("清空回收站成功");
    }

}
